package cn.bytecollege.controller;

import cn.bytecollege.model.Orders;
import cn.bytecollege.model.User;
import cn.bytecollege.model.UserAddress;
import cn.bytecollege.service.UserService;
import cn.bytecollege.vo.UserAddressVO;
import com.github.pagehelper.Page;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@Log
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户查看自己的订单
     * @param session
     * @return
     */
    @GetMapping("getOrder")
    public String getOrder(HttpSession session,Model model){

        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
//        Integer userId = 3;
        List<Orders> list = userService.getOrders(userId);
        model.addAttribute("list",list);
        return "pInformation";
    }
    @RequestMapping("getAllOrder")
    public String getAllOrder(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
//        Integer userId = 3;
        List<Orders> list = userService.getOrders(userId);
        model.addAttribute("list",list);
        return "pInformation :: ordersPageBox";
    }

    @RequestMapping("getOrderBy")
    public String getOrderBy(HttpSession session,String orderState,Model model){
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
//        Integer userId = 3;
        List<Orders> list = userService.getOrdersBy(userId,orderState);
        model.addAttribute("list",list);
        return "pInformation :: ordersPageBox";
    }

    /**
     * 用户删除自己的订单
     * @param orderId
     * @param
     * @param model
     * @return
     */
    @GetMapping("deleteOrder")
    public String delete(Integer orderId, Model model){
        int i = userService.delete(orderId);
        if (i==0){
            String msg = "该状态下的订单无法被删除";
            model.addAttribute("msg",msg);
            return "redirect:/user/getOrder";
        }
        return "redirect:/user/getOrder";
    }

    /**
     * 用户确认收货
     * @param orderId
     * @return
     */
    @GetMapping ("modById")
    public String mod(Integer orderId){
        int i = userService.mod(orderId);
        if (i==0){
        }
        return "redirect:/user/getOrder";
    }

    /**
     * 用户取消订单
     * @param orderId
     * @return
     */
    @GetMapping("/cancel")
    public String cancel(Integer orderId, HttpSession session) throws IOException {
        int i = userService.cancel(orderId);
        if (i==0){
            String msg = "该状态下订单无法取消";
            session.setAttribute("msg",msg);
            return "redirect:/user/getOrder";
        }else if (i==2){
            String msg = "订单已进入审核";
            session.setAttribute("msg",msg);
            return "redirect:/user/getOrder";
        }
        String msg = "订单取消成功";
        session.setAttribute("msg",msg);
//        PrintWriter writer = response.getWriter();
//        if(i==0){
//            writer.write("该状态下订单无法取消");
//        }else if (i==2){
//            writer.write("订单已进入审核");
//        } else if (i==1) {
//            writer.write("订单取消成功");
//        }
        return "redirect:/user/getOrder";
    }

    /**
     * 用户查看个人地址
     * @param
     * @return
     */
    @GetMapping("getAddresses")
    public String getAddresses(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
//        Integer userId = 1;
        List<UserAddress> list = userService.getAddresses(userId);
        model.addAttribute("list",list);
        model.addAttribute("jumpUrl","/user/modAddress?userAddressId=");
        model.addAttribute("jumpUrl1","/user/deleteAddress?userAddressId=");
        return "pAddress";
    }

    /**
     * 用户删除地址
     * @param userAddressId
     * @return
     */
    @GetMapping("deleteAddress")
    public String deleteAddress(Integer userAddressId){
        userService.deleteAddress(userAddressId);
        return "redirect:/user/getAddresses";
    }

    /**
     * 用户添加地址
     * @param session
     * @param userAddressVO
     * @param model
     * @return
     */
    @GetMapping("addAddress")
    public String addAddress(HttpServletRequest request, HttpSession session, UserAddressVO userAddressVO, Model model){
        if (userAddressVO.getAddress().equals("")||userAddressVO.getRealName().equals("")||userAddressVO.getTelephone().equals("")){
            return "forward:/user/getAddresses";
        }
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
//        Integer userId = 1;
        userAddressVO.setUserId(userId);
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userAddressVO,userAddress);
        userService.addAddress(userAddress);
        return "redirect:/user/getAddresses";

    }

    /**
     * 跳转到修改地址页面
     * @return
     */
    @GetMapping("modAddress")
    public String modAddress(Integer userAddressId,Model model){
        UserAddress userAddress = userService.modAddress(userAddressId);
        model.addAttribute("address",userAddress);
        return "modAddress";
    }

    /**
     * 修改用户地址功能
     * @param userAddressVO
     * @return
     */
    @PostMapping("modAddress")
    public String modAddress(UserAddressVO userAddressVO){
        log.info(userAddressVO.toString());
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userAddressVO,userAddress);
        userService.modAddress(userAddress);
        return "redirect:/user/getAddresses";
    }
}
